package ModelLayer.FileHandlers;

import java.util.ArrayList;

import ModelLayer.Sale;

/**
 * This class is an abstraction on top of BaseFileHandler, changes to the file that contains the
 * sales should only occur through the public methods of this class.
 * @author Tobias
 *
 */
public class SaleFileHandler extends BaseFileHandler
{

	public SaleFileHandler(String fileName)
	{
		super(fileName);
	}
	
	public void add(Sale sale)
	{
		ArrayList<Sale> sales = getAll();
		sales.add(sale);
		super.saveToFile(sales);
	}
	
	/**
	 * This method is used to update the stored Sales. All the Sales will be replaced with
	 * the ones in the passed ArrayList
	 * @param Sales
	 * The list of Sales to replace the existing ones.
	 */
	public void update(ArrayList<Sale> sales)
	{
		super.saveToFile(sales);
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Sale> getAll()
	{
		if(!fileExists()){
			return new ArrayList<Sale>();
		}
		
		return (ArrayList<Sale>)readFromFile();
	}
}
